package com.csair.seam.domain.model.entity;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Pattern;


import com.csair.seam.domain.service.IEquipOrgService;
import com.csair.seam.domain.service.IdleReceiveService;
import com.csair.seam.infrastructure.poi.annotation.ExportField;
import com.csair.seam.infrastructure.poi.annotation.ImportField;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel
@TableName("idle_receive")
public class IdleReceive  implements Serializable{/**
 *
 */

	@TableId("id")
	private Integer id;

	@ExportField(columnName = "原公司")
	@ImportField(name = "原公司",maxLength=25,transform = true)
	@ApiModelProperty(value="原公司")
	private String outcorp;

	@ImportField(name = "原使用部门",maxLength=25,transform=true)
	@ApiModelProperty(value="原使用部门")
	private String outdept;

	@ImportField(name = "接收公司",maxLength=25,transform = true)
	@ExportField(columnName="接收公司")
	@ApiModelProperty(value="接收公司")
	private String incorp;

	@ImportField(name = "接收使用部门",maxLength=35,transform=true)
	@ExportField(columnName="接收使用部门")
	@ApiModelProperty(value="接收使用部门")
	private String indept;

	@ImportField(name = "处置方式")
	@ExportField(columnName="处置方式")
	@ApiModelProperty(value="处置方式")
	private String intype;

	@ImportField(name = "设备大类",maxLength=25)
	@ExportField(columnName="设备大类")
	@ApiModelProperty(value="设备大类")
	private String category;

	@ImportField(name = "成交日期")
	@ExportField(columnName="成交日期")
	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	@ApiModelProperty(value="成交日期")
	private Date dealDate;

	@ImportField(name = "交易总额（元）",max = "999999999")
	@ExportField(columnName="交易总额（元）")
	@ApiModelProperty(value="交易总额（元）")
	private BigDecimal insum;

	@ImportField(name = "经办人",maxLength=25)
	@ExportField(columnName="经办人")
	@ApiModelProperty(value="经办人")
	private String operator;

	@ImportField(name = "联系方式",maxLength=20)
	@ExportField(columnName="联系方式")
	@ApiModelProperty(value="联系方式")
	private String phonenumber;

	@ImportField(name = "租期",max = "999999999")
	@ExportField(columnName="租期")
	@ApiModelProperty(value="租期")
	@TableField("rent_len")
	private Integer rentLen;

	@ImportField(name = "确认人",maxLength=25)
	@ExportField(columnName="确认人")
	@ApiModelProperty(value="确认人")
	private String confirman;

	@ImportField(name = "设备编码",maxLength=255)
	@ExportField(columnName="设备编码")
	@ApiModelProperty(value="对应NCC设备编码")
	private String equipcodes;

	@ImportField(name = "品牌",maxLength=25)
	@ExportField(columnName="品牌")
	@ApiModelProperty(value="品牌")
	private String  equipBrand;

	@ImportField(name = "型号",maxLength=25)
	@ExportField(columnName="型号")
	@ApiModelProperty(value="型号")
	private String equipModel;

	@ImportField(name = "使用年限",max = "32766")
	@ExportField(columnName="使用年限")
	@ApiModelProperty(value="使用年限")
	private Integer usedyear;

	@ImportField(name = "交易数量",max = "999999999")
	@ExportField(columnName="交易数量")
	@ApiModelProperty(value="交易数量")
	private Integer receivnum;

	@ApiModelProperty(value="操作人")
	private String creator;

	@ApiModelProperty(value="操作时间")
	@TableField("crea_time")
	private Date creaTime;

	@ApiModelProperty(value="展示id")
	private Integer showId;


	public boolean isIntypes(String intype){
		List<String> intypes = new ArrayList<>();
		intypes.add("内部交易（二级机构内部）");
		intypes.add("内部交易（二级机构之间）");
		intypes.add("对外出租");
		intypes.add("对外出售");
		intypes.add("报废处置");
		intypes.add("其他");
		return intypes.contains(intype);
	}

	public boolean  isCategorys(String category){
		List<String> categorys = new ArrayList<>();
		categorys.add("机务工具");
		categorys.add("IT设备");
		categorys.add("通用车辆");
		categorys.add("特种车辆");
		categorys.add("其他");
		return categorys.contains(category);
	}

	public Map<String,String> checkIdle(IEquipOrgService iEquipOrgService,IdleReceiveService idleReceiveService,IdleReceive ir){
		Map<String,String> errorMap = new HashMap<String,String>();
		checkType(ir,errorMap);
		if(!errorMap.isEmpty())
			return errorMap;
		checkMeum(ir,errorMap);

		if (StringUtils.isEmpty(idleReceiveService.getNameByCode(ir.getOutcorp()))){
			errorMap.put(ir.getOutcorp(), "查询不到公司");
		}
		if(StringUtils.isEmpty(iEquipOrgService.findOrgNameByDeptCode(ir.getOutcorp(),ir.getOutdept()))){
			errorMap.put(ir.getOutdept(), "该部门不在对应公司内");
		}

		if(errorMap.isEmpty()){
			String outcorp = iEquipOrgService.findOrgNameByCode(this.getOutcorp());
			if(outcorp.length() > 25){
				errorMap.put(this.getOutcorp(), outcorp+"=字段名称长度不能大于25");
			}
			String outdept = iEquipOrgService.findOrgNameByCode(this.getOutdept());
			if(outdept.length() > 25){
				errorMap.put(this.getOutdept(), outdept+"=字段名称长度不能大于25");
			}
			this.setOutcorp(outcorp);
			this.setOutdept(outdept);
		}
		if(StringUtils.equals(this.getIntype(),"内部交易（二级机构内部）")||StringUtils.equals(this.getIntype(),"内部交易（二级机构之间）")){
			if (StringUtils.isEmpty(idleReceiveService.getNameByCode(ir.getIncorp()))){
				errorMap.put(ir.getIncorp(), "查询不到公司");
			}
			if(StringUtils.isEmpty(iEquipOrgService.findOrgNameByDeptCode(ir.getIncorp(),ir.getIndept()))){
				errorMap.put(ir.getIndept(), "该部门不在对应公司内");
			}
			if(errorMap.isEmpty()){
				String incorp = this.getIncorp();
				String indept = this.getIndept();
				if(incorp.length() > 25){
					errorMap.put(this.getIncorp(), incorp+"=字段名称长度不能大于25");
				}
				if(indept.length() > 35){
					errorMap.put(this.getIncorp(), indept+"=字段名称长度不能大于35");
				}
				this.setIncorp(incorp);
				this.setIndept(indept);
			}
		}


		return errorMap;
	}


	public static List<IdleReceive> convert(List<String> corps){
		List<IdleReceive> irs = new ArrayList<>();
		corps.stream().forEach((e) ->{
			IdleReceive ir = new IdleReceive();
			ir.setOutcorp(e);
			irs.add(ir);
		});
		return irs;
	}
	public Map<String,String> checkType(IdleReceive ir,Map<String,String> errorMap){
		Class<? extends IdleReceive> irc = ir.getClass();
		Annotation[] annotations = irc.getAnnotations();
		Field[] ircFields = irc.getDeclaredFields();
		List<Field> fields =Arrays.asList(ircFields);
		for(Field field:ircFields){
			field.setAccessible(true);
			try {
				if(field.get(ir)==null) {
					if (field.getAnnotation(ImportField.class) != null) {
						field.getAnnotation(ImportField.class).name();
						errorMap.put(field.getAnnotation(ImportField.class).name(),"值不能null");
					}
				}else {
//					if(field.getAnnotation(ImportField.class) != null){
//						//field.getAnnotation(ImportField.class).maxLength();
//					}
				}

			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return errorMap;
	}
	public void checkMeum(IdleReceive ir,Map<String,String> errorMessage){
		if(!isIntypes(ir.getIntype())){
			errorMessage.put("处置方式","数值类型有误");
		}
		if(!isCategorys(ir.getCategory())){
			errorMessage.put("设备大类","数值类型有误");
		}
	}

}
